package yxy.game.pm2.command.hero;

import yxy.cherry.data.script.fomula;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.hero.HeroModule;

import java.util.Collection;

@MessageType(proto = Command.Heros, classtype = HeroMessage.HerosRequest.class)
public class CMD_Heros extends Command<HeroMessage.HerosRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.HerosRequest request) {

		String playerCode = messageSource.getTag();
		HeroMessage.HerosResponse.Builder response = HeroMessage.HerosResponse.newBuilder();
		HeroHandle heroHandle = new HeroHandle();
		HeroModule heroModule = HeroModule.get(playerCode);
		int times = heroModule.getHeroPackUpperBuyTimes();
		int costNum = fomula.武将背包每次增长花费.intValue(times + 1);

		Collection<Hero> heros = heroHandle.getHeros(playerCode);
		for (Hero hero : heros) {
			try {
				response.addHeros(heroHandle.getHeroSimple(playerCode, hero));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		response.setCost(costNum);
		response.setUpper(heroHandle.getUpper(playerCode));
		response.addAllTip(heroHandle.checkHeroTipInFormat(playerCode, FormatType.普通));

		send(messageSource, response, Result.Success);

	}

	private void send(MessageSource messageSource, HeroMessage.HerosResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
